package com.qms.repast;

import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.LogStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.qms.repast.commons.AppLocation;
import com.qms.repast.db.DaoMaster;
import com.qms.repast.db.DaoSession;
import com.qms.repast.utils.Density;
import com.qms.repast.utils.FileUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;

import org.greenrobot.greendao.database.Database;

import cn.jiguang.analytics.android.api.JAnalyticsInterface;
import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.PlatformConfig;
import cn.jpush.android.api.JPushInterface;

/**
 * @author 泅渡者
 * @date 2018/8/29 0029
 * @describe:
 */
public class App extends Application {
	private static final String TAG = "REPAST";
	public static final String DB_NAME = "qms.db";
	private static App instance;
	private static Context context;
	protected static Handler handler;
	private static int mainThreadId;
	private RefWatcher mRefWatcher;
	private static DaoSession mDaoSession;
	public static final boolean ENCRYPTED = true;
	static {
		//static 代码段可以防止内存泄露
		SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
			@Override
			public RefreshHeader createRefreshHeader (Context context, RefreshLayout layout) {
				layout.setPrimaryColorsId(R.color.colorWhite, android.R.color.black);
				return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);
			}
		});
		//设置全局的Footer构建器
		SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
			@Override
			public RefreshFooter createRefreshFooter (Context context, RefreshLayout layout) {
				//指定为经典Footer，默认是 BallPulseFooter
				return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
			}
		});
	}

	@Override
	public void onCreate () {
		super.onCreate();
		instance = this;
		context = instance.getApplicationContext();
		mainThreadId = android.os.Process.myTid();
		handler = new Handler();
		mRefWatcher = LeakCanary.install(this);
		//日志初始化
		initLogger();
		//文件初始化
		FileUtil.init();
		//极光推送初始化
		JPushInterface.setDebugMode(true);
		JPushInterface.init(this);
		//极光分享初始化
		JShareInterface.setDebugMode(BuildConfig.DEBUG);
		PlatformConfig platformConfig = new PlatformConfig()
				.setWechat("wxa366a127d520f314", "e71d2eebe2a615cfd43f7d49c089f1ea");
		JShareInterface.init(this, platformConfig);
		//极光统计初始化
		JAnalyticsInterface.setDebugMode(BuildConfig.DEBUG);
		JAnalyticsInterface.init(this);
		JAnalyticsInterface.initCrashHandler(this);
		//高德地图初始化
		AppLocation.init(this);
		//初始化数据库
		initGreenDao(this);
		Density.setDensity(this, 360);
	}

	/**
	 * Application 单例
	 * @return
	 */
	public static App getInstance () {
		return instance;
	}

	/**
	 * Application 上下文
	 * @return
	 */
	public static Context getContext () {
		return context;
	}

	/**
	 * 获取主线程Id
	 * @return
	 */
	public static int getMainThreadId () {
		return mainThreadId;
	}

	public static Handler getHandler () {
		return handler;
	}

	/**
	 * 初始化数据库
	 * @param context
	 */
	private void initGreenDao (Context context) {
		DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, ENCRYPTED ? "notes-db-encrypted" : DB_NAME);
		Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();

		DaoMaster daoMaster = new DaoMaster(db);
		mDaoSession = daoMaster.newSession();
	}

	public static DaoSession getmDaoSession () {
		return mDaoSession;
	}

	/**
	 * 初始化Logger
	 */
	private void initLogger () {
		LogStrategy logStrategy = new LogStrategy() {

			private String[] prefix = {". ", " ."};
			private int index = 0;

			@Override
			public void log (int priority, @Nullable String tag, @NonNull String message) {
				index = index ^ 1;
				Log.println(priority, prefix[index] + tag, message);
			}
		};

		FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
				.logStrategy(logStrategy)
				.showThreadInfo(true)
				.methodCount(0)
				.methodOffset(7)
				.tag(TAG)
				.build();
		Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
	}
}
